import { MockMethod } from 'vite-plugin-mock';
import article from '../json/article.json';

export default [
  {
    url: '/dev-api/article/list',
    method: 'get',
    response: (options: any) => {
      const pageNum = Number(options.query.pageNum);
      const pageSize = Number(options.query.pageSize);
      const start = (pageNum - 1) * pageSize;
      const end = pageNum * pageSize;
      return {
        list: article.list.slice(start, end),
        total: article.list.length,
      };
    },
  },
  {
    url: '/dev-api/collectionWorks/list',
    method: 'get',
    response: (options: any) => {
      const pageNum = Number(options.query.pageNum);
      const pageSize = Number(options.query.pageSize);
      const start = (pageNum - 1) * pageSize;
      const end = pageNum * pageSize;
      const list = article.list.filter((J) => J.type == 'CollectionWorks');
      return {
        list: list.slice(start, end),
        total: list.length,
      };
    },
  },
  {
    url: '/dev-api/studyNotes/list',
    method: 'get',
    response: (options: any) => {
      const pageNum = Number(options.query.pageNum);
      const pageSize = Number(options.query.pageSize);
      const start = (pageNum - 1) * pageSize;
      const end = pageNum * pageSize;
      const list = article.list.filter((J) => J.type == 'StudyNotes');
      return {
        list: list.slice(start, end),
        total: list.length,
      };
    },
  },
  {
    url: '/dev-api/tags/detail',
    method: 'get',
    response: (options: any) => {
      const pageNum = Number(options.query.pageNum);
      const pageSize = Number(options.query.pageSize);
      const name = options.query.name;
      const start = (pageNum - 1) * pageSize;
      const end = pageNum * pageSize;
      const list = article.list.filter((J) => {
        return J.tag.indexOf(name) != -1;
      });
      return {
        list: list.slice(start, end),
        total: list.length,
      };
    },
  },
  {
    url: '/dev-api/article/detail',
    method: 'post',
    response: (options: any) => {
      const form = options.body;
      const dataList = article.list.filter((J) => J.route == form.route);
      return {
        data: dataList[0],
      };
    },
  },
] as MockMethod[]; // 这里其实就是定义数据格式的，不了解的同学可以参考typescript的官方文档
